package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnFarmproduce;
import com.example.zhunongzonghe.entity.ZnSort;
import com.example.zhunongzonghe.service.IZnFarmproduceService;
import com.example.zhunongzonghe.service.IZnSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/znSort")
public class ZnSortController  {
    @Autowired
    private IZnSortService znSortService;
    @Autowired
    private IZnFarmproduceService znFarmproduceService;

    @GetMapping("/queryAllSort")
    public Result<List<ZnSort>> QueryAllSort(){
        List<ZnSort> znSortList = znSortService.list();
        return new Result<>(true,"查询成功",200,znSortList);
    }

    @PostMapping("/addSort")
    public Result<ZnSort> addSort(@RequestBody ZnSort sort){
        znSortService.save(sort);
        return new Result<>(true,"添加成功",200);
    }

    @DeleteMapping("/deleteSort/{id}")
    public Result<ZnSort> deleteSort(@PathVariable Integer id){
        LambdaQueryWrapper<ZnSort> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnSort::getSortId,id);
        LambdaQueryWrapper<ZnFarmproduce> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnFarmproduce::getSortId,id);
        List<ZnFarmproduce> list = znFarmproduceService.list(wrapper);
        list.forEach(znFarmproduce -> {
            LambdaQueryWrapper<ZnFarmproduce> znFarmWrapper = new LambdaQueryWrapper<>();
            znFarmWrapper.eq(ZnFarmproduce::getSortId,id);
            znFarmproduceService.remove(znFarmWrapper);
        });
        znSortService.remove(queryWrapper);
        return new Result<>(true,"删除成功",200);
    }
}